package com.ruoyi.patrol.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PatrolConstans;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.patrol.domain.*;
import com.ruoyi.patrol.domain.vo.PtSiteRecordVo;
import com.ruoyi.patrol.domain.vo.PtStatisticsVo;
import com.ruoyi.patrol.domain.vo.ScheduleInfo;
import com.ruoyi.patrol.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 数据统计Controller
 * 
 * @author mlb
 * @date 2022-11-23
 */
@RestController
    @RequestMapping("/patrol/statistics")
public class PtStatisticsController extends BaseController
{
    @Autowired
    private IPtLineGuardService ptLineGuardService;
    @Autowired
    private IPtLineStaffService ptLineStaffService;
    @Autowired
    private IPtLineRecordDetailService ptLineRecordDetailService;
    @Autowired
    private IPtSiteRecordStaffService ptSiteRecordStaffService;

    /**
     * 获取巡查线路详细信息
     */
    /** @PreAuthorize("@ss.hasPermi('patrol:line:query')") */
    @GetMapping(value = { "schedule"})
    public AjaxResult getScheduleStatistics(String date) throws ParseException {

        // 保安排班统计查询条件
        PtLineGuard query = new PtLineGuard();
        // 行政排班统计查询条件
        PtLineStaff staffQuery = new PtLineStaff();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (date == null) {
            query.setDate(sdf.parse(sdf.format(new Date())));
            staffQuery.setDate(sdf.parse(sdf.format(new Date())));
        } else {
            query.setDate(sdf.parse(date));
            staffQuery.setDate(sdf.parse(date));
        }

        //分别查询保安排班和行政排班总数
        long total = ptLineGuardService.selectCount(query);
        long staffTotal = ptLineStaffService.selectCount(staffQuery);

        //分别查询保安排班和行政排班未完成的数量
        query.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DONE);
        long finishedCount = ptLineGuardService.selectCount(query);

        staffQuery.setPatrolStatus(PatrolConstans.PATROL_SCHEDULE_STATUES_DONE);
        long staffFinishedCount = ptLineStaffService.selectCount(staffQuery);


        // 数据汇总
        PtStatisticsVo.PatrolStatusStatistics statistics = new PtStatisticsVo.PatrolStatusStatistics();
        statistics.setFinishedCount(finishedCount + staffFinishedCount);
        statistics.setTotal(total + staffTotal);
        statistics.setUnfinishedCount(statistics.getTotal() - statistics.getFinishedCount());

        return AjaxResult.success(statistics);
    }

    /**
     * 获取巡查线路详细信息
     */
    /** @PreAuthorize("@ss.hasPermi('patrol:line:query')") */
    @GetMapping(value = { "siteRecord"})
    public AjaxResult getSiteRecordStatistics(String date) throws ParseException {
        // 保安巡查记录条件
        PtLineRecordDetail query = new PtLineRecordDetail();
        // 保安巡查记录条件
        PtSiteRecordStaff staffQuery = new PtSiteRecordStaff();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (date == null) {
            query.setDate(sdf.parse(sdf.format(new Date())));
            staffQuery.setDate(sdf.parse(sdf.format(new Date())));
        } else {
            query.setDate(sdf.parse(date));
            staffQuery.setDate(sdf.parse(date));
        }

        long total = ptLineRecordDetailService.selectCount(query);
        long staffTotal = ptSiteRecordStaffService.selectCount(staffQuery);

        query.setState(PatrolConstans.PATROL_SITE_STATUES_ABNORMAL);
        long abnormalCount = ptLineRecordDetailService.selectCount(query);
        staffQuery.setState(PatrolConstans.PATROL_SITE_STATUES_ABNORMAL);
        long staffAbnormalCount = ptSiteRecordStaffService.selectCount(staffQuery);

        query.setState(PatrolConstans.PATROL_SITE_STATUES_HANDLED);
        long handledCount = ptLineRecordDetailService.selectCount(query);
        staffQuery.setState(PatrolConstans.PATROL_SITE_STATUES_HANDLED);
        long staffHandledCount = ptSiteRecordStaffService.selectCount(staffQuery);

        PtStatisticsVo.SiteStatusStatistics statistics = new PtStatisticsVo.SiteStatusStatistics();
        statistics.setUnHandleCount(abnormalCount + staffAbnormalCount);
        statistics.setHandledCount(handledCount + staffHandledCount);

        return AjaxResult.success(statistics);
    }

    /**
     * 查询未完成巡查的学校和线路
     */
    @GetMapping("/undoneSchedule")
    public List<ScheduleInfo> listUndoneSchedule(ScheduleInfo scheduleInfo) throws ParseException {
        if (scheduleInfo.getScheduleDate() == null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            scheduleInfo.setScheduleDate(sdf.parse(sdf.format(new Date())));
        }
        scheduleInfo.setSchoolId(SecurityUtils.getSchoolId());
        List<ScheduleInfo> list = ptLineGuardService.listUndone(scheduleInfo);
        list.stream().forEach(g -> g.setType("guard"));
        List<ScheduleInfo> staffList = ptLineStaffService.listUndone(scheduleInfo);
        staffList.stream().forEach(s -> s.setType("staff"));
        list.addAll(staffList);
        return list;
    }
}
